'''更新操作处理'''

# 当RT矩阵中的值改变后，对schemes中的课程信息进行更改

import config
import load


def updated_scheme(scheme,new_position,task_num):
    index = [index for index,item in enumerate(scheme) if item[2] == task_num][0]
    scheme[index] = list(scheme[index])
    scheme[index][5] = new_position
    scheme[index] = tuple(scheme[index])
    return scheme
def updated_matrixa(matrix_c, positions_c, new_positions_c,task_num):
    room_times = [(i,room_time) for i,room_time in enumerate(matrix_c) if room_time[0] == positions_c[0][0]]
    room_times1 = [(j,room_time1) for j,room_time1 in enumerate(matrix_c) if room_time1[0] == new_positions_c[0][0]]
    room_time = list(room_times[0][1])
    room_time1 = list(room_times1[0][1])
    for room_tim in room_time[2:][:]:
        if room_tim[1]== task_num:
            room_time.remove(room_tim)
            break
    room_time1.append((new_positions_c[1],task_num))
    tuple(room_time)
    tuple(room_time1)
    matrix_c[room_times[0][0]] = room_time
    matrix_c[room_times1[0][0]] = room_time1
    return  matrix_c

# def update_stu_tea_list(task, data_list):
#     # 要更新的教室时间片信息
#     new_task_info = []  # 新的教室和时间信息
#     for tuple in task[11:]:
#         new_tuple = (task[0], tuple[0], tuple[1])
#         new_task_info.append(new_tuple)
#
#     # 遍历 student_list_copy 更新学生的课程信息
#     for data_info in data_list:
#         # 找到匹配的课程名称 ,更换成新的教室时间组合
#         if task[0] in data_info[1]:
#             for i, room_time in enumerate(data_info[2:]):
#                 if room_time[0] == task[0]:
#                     data_info[i + 2:i + 2 + len(new_task_info)] = new_task_info
#                     break
#             continue
#     return data_list


# 更新个体最优
def update_pbest(in_, RT_, fitness_, best_p, RT_p, fitness_p):
    for i in range(len(in_)):
        # 通过比较历史pbest和当前粒子适应值，决定是否需要更新pbest的值。
        if compare_pbest(fitness_[i], fitness_p[i]):
            fitness_p[i] = fitness_[i]
            best_p[i] = in_[i]
            RT_p[i] = RT_[i]
    return best_p, RT_p, fitness_p


# 比较个体历史最优，更新个体历史最优
def compare_pbest(in_indiv, pbest_indiv):
    num_greater = 0
    num_less = 0
    for i in range(len(in_indiv)):
        if in_indiv[i] < pbest_indiv[i]:
            num_greater = num_greater + 1
        if in_indiv[i] > pbest_indiv[i]:
            num_less = num_less + 1
    # 如果当前粒子支配历史pbest，则更新,返回True
    if (num_greater > 0 and num_less == 0):
        return True
    # 如果历史pbest支配当前粒子，则不更新,返回False
    elif (num_greater == 0 and num_less > 0):
        return False
    else:
        # 如果互相支配按权重决定
        lion_a = pbest_indiv[0] * config.room_weight + pbest_indiv[1] * config.time_weight + pbest_indiv[
            2] * config.s_weight
        lion_b = in_indiv[0] * config.room_weight + in_indiv[1] * config.time_weight + in_indiv[2] * config.s_weight
        if lion_a > lion_b:
            return True
        else:
            return False


def update_pbest_RTMatrix(schemep, classroom_list):
    RTlist = []
    for i in range(len(schemep)):
        task_list_copy = schemep[i]
        RT = load.update_RTmatrix(task_list_copy, classroom_list)
        RTlist.append(RT)
    return RTlist
